<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/23
+--------------------------------------------------------------
 * Time: 10:29
+--------------------------------------------------------------
 * 短消息
+--------------------------------------------------------------
 */
class MailAction extends BaseAction{

    protected $page_size = 10;

    public function _initialize(){
        self::$action = -1;
        parent::_initialize();
        //检查用户是否登录
        if(empty($_SESSION['user_id'])){
            $this->redirect('Index/index');
        }
    }
    /**
     * 显示用所有短消息
     */
    public function index(){

        $type = $this->_get('type');//按标签过滤数据

        $read_where     = " to_user='".$_SESSION['user_id']."' and new_mail=0 and defunct='N'";
        $unread_where   = " to_user='".$_SESSION['user_id']."' and new_mail=1 and defunct='N'";
        $sent_where     = " from_user='".$_SESSION['user_id']."' and defunct='N'";
        $total_where = " defunct='N' and (to_user='".$_SESSION['user_id']."' or from_user='".$_SESSION['user_id']."') ";

        switch(intval($type)){
            case 1://已读
                $where = $read_where;
                break;
            case 2://未读
                $where = $unread_where;
                break;
            case 3://已发送
                $where = $sent_where;
                break;
            default://全部
                $where = $total_where;
                break;
        }

        $read   = D('Mail')->where($read_where)->count();
        $unread = D('Mail')->where($unread_where)->count();
        $sent   = D('Mail')->where($sent_where)->count();
        $total  = D('Mail')->where($total_where)->count();

        $count = D('Mail')->where($where)->count();
        vendor('Page');

        $page = new Page(intval($count),$this->page_size);
        $sql  = "select *from mail where {$where} order by in_date DESC limit ".$page->firstRow.",".$page->listRows;
        $mail_list = D('Mail')->query($sql);
        $this->assign('mail_list',$mail_list);

        $this->assign('type',intval($_GET['type']));
        $this->assign('page',$page);
        $this->assign('read',$read);
        $this->assign('unread',$unread);
        $this->assign('sent',$sent);
        $this->assign('total',$total);

        $user_info = D('Users')->returnUser();
        $this->assign('solution',$user_info['solution_info']);
        $this->assign('user',$user_info['user_info']);
        $this->display('index');
    }

    /**
     * 发短消息
     */
    public function add(){
        if($this->_post()){
            $post = $this->_post();
            $is = $this->checkUserid($post['to_user']);
            if(!$is['status']){
                $this->error($is['info']);
            }
            $res = D('Mail')->sendMail($post);
            if($res['status']){
                $this->success('发送成功',U('Mail/index?type=3'));
            }else{
                $this->error('发送失败');
            }
        }else{

            $user_info = D('Users')->returnUser();
            $this->assign('solution',$user_info['solution_info']);
            $this->assign('user',$user_info['user_info']);

            $this->display('add');
        }

    }

    /**
     * 查看短消息
     */
    public function view(){

        $mail_id = $this->_get('id');
        $mail_id = !empty($mail_id) ? intval($mail_id) : $this->error('错误操作');
        $res = D('Mail')->where("mail_id=".$mail_id." and (to_user='".$_SESSION['user_id']."' or from_user='".$_SESSION['user_id']."') ")->select();

        if(empty($res)){
            $this->error('无权限');
        }
        D('Mail')->where("mail_id=".$mail_id." and (to_user='".$_SESSION['user_id']."' or from_user='".$_SESSION['user_id']."') ")->setField('new_mail',0);
        $user_info = D('Users')->returnUser();
        $this->assign('solution',$user_info['solution_info']);
        $this->assign('user',$user_info['user_info']);
        $this->assign('info',$res[0]);
        $this->display('view');
    }
    /**
     * 删除短消息
     */
    public function delete(){
        $mail_id = $this->_get('id');
        $mail_id = !empty($mail_id) ? intval($mail_id) : $this->error('错误操作');
        $res = D('Mail')->where("mail_id=".$mail_id." and (to_user='".$_SESSION['user_id']."' or from_user='".$_SESSION['user_id']."') ")->select();
        if(!empty($res)){
           $is_success =  D('Mail')->where('mail_id='.$mail_id)->setField('defunct','Y');
            if(false!==$is_success){
                $this->success('删除成功');
            }else{
                $this->success('删除失败');
            }
        }else{
            $this->error('无权限');
        }
        exit;
    }

    public function reply(){
        $user_id = $this->_get('id');
        $return = $this->checkUserid($user_id);
        $user_id = !empty($return['status']) ?  $user_id : $this->error($return['info']);

        $user_info = D('Users')->returnUser();
        $this->assign('solution',$user_info['solution_info']);
        $this->assign('user',$user_info['user_info']);

        $this->assign('user_id',$user_id);
        $this->display('add');
    }
    /**
     * 异步检查用户id是否存在
     */
    public function ajaxCheck(){
        $user_id = $this->_post('user_id');
        $return = $this->checkUserid($user_id);

        $this->ajaxReturn($return);
    }

    /**
     * @param $user_id
     * @return array
     * 发送短消息时检查用户id是否存在
     */
    public function checkUserid($user_id){
        $return = array('status'=>'','info'=>'');

        if(empty($user_id)){
            $return['status'] = false;
            $return['info']   = "Not allowed to be empty";
        }else{
            $res = D('Users')->where("user_id='$user_id'")->limit(1)->select();
            if(!empty($res)){
                $return['status'] = true;
                $return['info']   = "allow";
            }else{
                $return['status'] = false;
                $return['info']   = "User ID does not exist";
            }
        }
        return $return;
    }
}